package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.PayBackProjectAdapter
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.PBDetailBean
import com.hinnotek.amoeba.databinding.ActivityPayBackModifyBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ClickUtil
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.ContractViewModel
import com.hinnotek.amoeba.dialog.DelCustomDialog
import com.lee.common.network.net.ObserverState
import com.xuexiang.xutil.common.StringUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PayBackModifyActivity : BaseActivity<ActivityPayBackModifyBinding>(), View.OnClickListener {
    private var adapter1: PayBackProjectAdapter? = null
    private var companyContractId = 0
    private var pd_surplus = 0.0

    private val viewModel by viewModels<ContractViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("回款")

        setContentView(R.layout.activity_pay_back_modify)
    }

    override fun initVew() {
        findViewById<View>(R.id.superButton_complete).setOnClickListener(this)
        findViewById<View>(R.id.superButton_save).setOnClickListener(this)
    }

    override fun setData() {
        val manager1 = LinearLayoutManager(mContext)
        manager1.orientation = RecyclerView.VERTICAL

        bindingView.rlvPayBackPrice.setLayoutManager(manager1)
        bindingView.rlvPayBackPrice.setAdapter(PayBackProjectAdapter(R.layout.item_pay_back_gridview).also {
            adapter1 = it
        })
        companyContractId = intent.getIntExtra("companyContractId", 0)
        getCompanyContractByIdForUnComplete(companyContractId)
    }

    override fun onClick(v: View) {
        if (ClickUtil.isFastClick()) {
            return
        }
        when (v.id) {
            R.id.superButton_complete -> {
                val delCustomDialog =
                    DelCustomDialog(mContext)
                delCustomDialog.setContent("回款完成后不能再次回款！")
                delCustomDialog.setOnClickBottomListener(object :
                    DelCustomDialog.OnClickBottomListener {
                    override fun onPositiveClick() {
                        updateCompanyContractBackStateComplete(companyContractId)
                    }

                    override fun onNegtiveClick() {
                        delCustomDialog.dismiss()
                    }
                })
                delCustomDialog.show()
            }
            R.id.superButton_save -> {
                val backMoney = bindingView.etPayPrice.text.trim()
                if (StringUtils.isEmpty(backMoney)) {
                    ToastUtils.show(mContext, "请填写回款金额")
                    return
                }
                if (backMoney.toString().toDouble()== 0.0) {
                    ToastUtils.show(mContext, "回款金额不能等于0")
                    return
                }
                if (backMoney.toString().toDouble() > pd_surplus) {
                    ToastUtils.show(mContext, "回款金额不能大于回款剩余金额")
                    return
                }
                addCompanyContractBack(companyContractId, backMoney.toString())
            }
        }
    }

    /**
     * app端回款使用的通过Id获取单个合同回款详情接口
     */
   private fun getCompanyContractByIdForUnComplete(companyContractId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getCompanyContractByIdForUnComplete(companyContractId).observe(this,object: ObserverState<PBDetailBean>(){
            override fun onDataChange(data: PBDetailBean) {
                customDialog.dismiss()
                bindingView.tvContractName.setText(data.contractTypeName)
                bindingView.tvUnit.setText(data.signUnit)
                bindingView.tvContractPrice.setText(data.signMoney)
                bindingView.tvPdTotal.setText(data.backMoney)
                pd_surplus = data.signMoney.toDouble() - data.backMoney.toDouble()
                bindingView.tvPdSurplus.text = pd_surplus.toString() + ""
                adapter1?.setList(data.companyContractBacks)
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })


    }

    /**
     * app端添加回款记录接口
     */
    private fun addCompanyContractBack(companyContractId: Int, backMoney: String) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.addCompanyContractBack(companyContractId,backMoney).observe(this,object :ObserverState<BaseBean>(){
            override fun onDataChange(data: BaseBean) {
                customDialog.dismiss()
                finishActivity()
                super.onDataChange(data)
            }
            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })
    }

    /**
     * app端更新合同回款状态为已完成接口
     */
    private fun updateCompanyContractBackStateComplete(companyContractId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        viewModel.updateCompanyContractBackStateComplete(companyContractId).observe(this,object :ObserverState<BaseBean>(){
            override fun onDataChange(data: BaseBean) {
                customDialog.dismiss()
                finishActivity()
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })
    }
}